﻿using B.S.Base.Write.API.Application.Command;
using MediatR;
using WEBAPI.Damoin;
using WEBAPI.ErroeCode;
using WEBAPI.Indaseture.Infasetures;

namespace B.S.Base.Write.API.Application.CommandHandLer
{
    public class DeleteMenuCommandHandLer : IRequestHandler<DeleteMenuCommand, APIResult<object>>
    {
        private readonly IBaseRepostiy<MenuModel> menu;

        public DeleteMenuCommandHandLer(IBaseRepostiy<MenuModel> menu)
        {
            this.menu = menu;
        }
        public Task<APIResult<object>> Handle(DeleteMenuCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var list = menu.GetAll().FirstOrDefault(x => x.Id == request.Id);
            if (list != null)
            {
                result.Code = ResultCode.Ok;
                result.Message = ResultCode.Ok.ToString();
                list.IsDeleted = true;
                Del(list.Id);
                menu.Update(list);
            }
            return Task.FromResult(result);
        }
        public string Del(int id)
        {
            List<MenuModel> subset = menu.GetAll().Where(x => x.Pid == id).ToList();
            if (subset != null)
            { 
                foreach (var sub in subset)
                {
                    sub.IsDeleted = true;
                    Del(sub.Id);
                }
            }
            return "删除成功";
        }

    }
}
